/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation:  14th/Sep/2007                                             
 * Date of last modification: 19th/Sep/2007
 *                                                                
 * Description: provides a convenient method to synchronize 3 objects
 * (item, FileInfo, Comment) of an the same item (same itemId)
 ******************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Pbdms.Common.DataObject;

namespace Pbdms.BusinessLogic
    
{
    /// <summary>
    /// a class provide services to synchronize different objects derived from the same
    /// document item (comment object, fileInfo object and item object)
    /// </summary>
    public class ItemSynchronizer
    {
        /// <summary>
        /// select the suitable id to assign for those object and save into database
        /// </summary>
        /// <param name="itemObj"></param>
        /// <param name="commentObj"></param>
        /// <param name="fileInfoObj"></param>
        /// <returns></returns>
        public static String AppendNewItem( Item itemObj, Comment commentObj, FileInfo fileInfoObj)
        {
            string newItemID = "";
            //make id consistent and valid name
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Item));
            System.IO.StringWriter str = new System.IO.StringWriter();
            serializer.Serialize(str, itemObj);

            newItemID = Common.Global.computeMessageDigest(str.ToString() + DateTime.Now.ToString());
            itemObj.Id = newItemID;
            commentObj.Id = newItemID;
            fileInfoObj.ID = newItemID;

            //append to database
            DataController.ItemController.create().appendItem(itemObj);
            DataController.CommentController.create().appendComment(commentObj);
            DataController.FileInfoController.create().appendFileInfo(fileInfoObj);

            return newItemID;
        }

        /// <summary>
        /// delete an item - requires to delete parts of 3 xml files which present data of the same item
        /// </summary>
        /// <param name="itemID"></param>
        public static void DeleteItem(String itemID)
        {
            DataController.ItemController.create().deleteItem(itemID);
            DataController.FileInfoController.create().deleteFileInfo(itemID);
            DataController.CommentController.create().deleteComment(itemID);

        }

    }
}
